﻿@using BootstrapBlazor.Components
@using Microsoft.AspNetCore.SignalR.Client
@using TrackSystem.IO.Formula
@using TrackSystem.IO.Loops

<div class="main-container">
    @* -------- PID回路的参数显示及设定------------------- *@
    <div class="left-panel">
        <Card Color="Color.Primary">
            <HeaderTemplate>
                <div class="col-6 col-sm-6 col-md-6 col-lg-6">
                    调节回路 @loop.Name
                </div>
                <div class="col-6 col-sm-6col-md-6 col-lg-6">
                    <Switch OnColor="Color.Success" OnText="使能" OffText="关闭"
                            @bind-value="@loop.Enabled"
                            OnValueChanged="@((a) => SetValue((loop.Name ??= "") + "_Enable", a))"/>
                </div>
            </HeaderTemplate>

            <BodyTemplate>
                <Row ItemsPerRow="ItemsPerRow.Two">
                    <Switch OnColor="Color.Success" OnText="手动" OffText="自动"
                            @bind-value="@loop.Manual"
                            OnValueChanged="@((a) => SetValue((loop.Name ??= "") + "_Manual", a))"/>
                    <Switch OnColor="Color.Success" OnText="正作用" OffText="反作用"
                            @bind-value="@loop.Direction"
                            OnValueChanged="@((a) => SetValue((loop.Name ??= "") + "_Dir", a))"/>
                </Row>

                <Divider/>

                <Row ItemsPerRow="ItemsPerRow.Two">
                    <p class="card-text">过程值 @loop.PVName : </p>
                    <p class="card-text"> @loop.PV.ToString("0.0000") </p>

                    <p class="card-text text-info" onclick="@(() => ChangValueDouble(loop.SVName, loop.SV))">设定值 @loop.SVName :</p>
                    <p class="card-text" onclick="@(() => ChangValueDouble(loop.Name + "_SV", loop.SV))"> @loop.SV.ToString("0.0000") </p>

                    <p class="card-text text-info" onclick="@(() => ChangValueDouble(loop.Name + "_OP", loop.OP))">输出值 @loop.OPName : </p>
                    <p class="card-text">@loop.OP.ToString("0.0000") </p>
                </Row>

                <Divider/>

                <Row ItemsPerRow="ItemsPerRow.Two">
                    <p class="card-text text-info" onclick="@(() => ChangValueInt(loop.Name + "_ULmt", loop.ULmt))">输出上限: @loop.ULmt</p>
                    <p class="card-text text-info" onclick="@(() => ChangValueInt(loop.Name + "_LLmt", loop.LLmt))">输出下限: @loop.LLmt</p>

                    <p class="card-text text-info" onclick="@(() => ChangValueInt(loop.Name + "_NDZone", loop.NDeadZone))">反向死区: @loop.NDeadZone %</p>
                    <p class="card-text text-info" onclick="@(() => ChangValueInt(loop.Name + "_PDZone", loop.PDeadZone))">正向死区: @loop.PDeadZone %</p>

                    <p class="card-text text-info" onclick="@(() => ChangValueDouble(loop.Name + "_P", loop.PVal))">比列参数: @loop.PVal.ToString("0.0000") </p>
                    <p class="card-text text-info" onclick="@(() => ChangValueDouble(loop.Name + "_I", loop.IVal))">积分参数: @loop.IVal.ToString("0.0000") </p>
                    <p class="card-text text-info" onclick="@(() => ChangValueDouble(loop.Name + "_D", loop.DVal))">微分参数: @loop.DVal.ToString("0.0000")</p>
                </Row>

                <Divider/>

                <Row ItemsPerRow="ItemsPerRow.Three">
                    <Button Text="普通PID"
                            IsOutline="@(loop.EnabledAdvanced != 0)"
                            onclick="@(() => OnAdvancedChanged(0))"
                            Color="Color.Secondary">
                    </Button>
                    <Button Text="积分分离法"
                            IsOutline="@(loop.EnabledAdvanced != 1)"
                            onclick="@(() => OnAdvancedChanged(1))"
                            Color="Color.Secondary">
                    </Button>
                    <Button Text="位置式PID"
                            IsOutline="@(loop.EnabledAdvanced != 2)"
                            onclick="@(() => OnAdvancedChanged(2))"
                            Color="Color.Secondary">
                    </Button>
                </Row>

                @if (loop.EnabledAdvanced == 1)
                {
                    <Divider/>
                    <Row ItemsPerRow="ItemsPerRow.Two">
                        <p class="card-text text-info" onclick="@(() => ChangValueInt(loop.Name + "_DeltaP1", loop.DeltaPoint1))">第一积分分离点: @loop.DeltaPoint1</p>
                        <p class="card-text text-info" onclick="@(() => ChangValueInt(loop.Name + "_DeltaP2", loop.DeltaPoint2))">第二积分分离点: @loop.DeltaPoint2</p>
                    </Row>
                }
                @if (loop.EnabledAdvanced == 2)
                {
                    <Divider/>
                    <Row ItemsPerRow="ItemsPerRow.Two">
                        <p class="card-text text-info" onclick="@(() => ChangValueInt(loop.Name + "_DeltaP1", loop.DeltaPoint1))">第一位置点: @loop.DeltaPoint1</p>
                        <p class="card-text text-info" onclick="@(() => ChangValueInt(loop.Name + "_DeltaP2", loop.DeltaPoint2))">第二位置点: @loop.DeltaPoint2</p>
                    </Row>
                }

                <Divider/>
                <Row ItemsPerRow="ItemsPerRow.Four">
                    <Button Text="正常运行"
                            IsOutline="@(loop.AutoTuned != 0)"
                            onclick="@(() => OnAutoTunedChanged(0))"
                            Color="Color.Secondary">
                    </Button>
                    <Button Text="开始整定"
                            IsOutline="@(loop.AutoTuned != 1)"
                            onclick="@(() => OnAutoTunedChanged(10))"
                            Color="Color.Secondary">
                    </Button>
                    <Button Text="自整定中"
                            IsOutline="@(loop.AutoTuned != 2)"
                            Color="Color.Secondary">
                    </Button>
                    <Button Text="整定结束"
                            IsOutline="@(loop.AutoTuned != 3)"
                            Color="Color.Secondary">
                    </Button>
                </Row>
            </BodyTemplate>

            @* ---------------------------------------------------------------------------------------- *@

            <FooterTemplate>
                <Button class="btn btn-primary" onclick="DataSaveToTable">数据保存</Button>
                <Button class="btn btn-info" onclick="OnClearData">清除曲线</Button>
            </FooterTemplate>
        </Card>
    </div>

    @* ------过程值、设定值、输出值的趋势图---------------------- *@
    <div class="right-container">
        <div class="top-section">
            <Chart @ref="_svpvChart" IsAnimation="false" OnInitAsync="OnInitContinue1" Responsive="true"/>
        </div>
        <div class="bottom-section">
            <Chart @ref="_opChart" IsAnimation="false" OnInitAsync="OnInitContinue2" Responsive="true"/>
        </div>
    </div>
</div>
